<?php
/*
 * Created on 2012-8-22
 * Creater :cnluckylee
 * Creater mail:cnlucklee@gmail.com
 */
 class cornModel extends Model{


        public function saveClassName($data){
                $db=$this->db;
                foreach($data as $k=>$v)
                {
                	$db->insert('article_Class',$v);
                }
        }

        public function showUrl($id=null)
        {
ob_start();

ob_implicit_flush(true);
        	$db=$this->db;
        	$where = '';
        	if($id>0)
        	$where = " where id=".intval($id);
        	$rs = $db->findall("article_Class",$where,'id');
        	$curl_muilt = $this->load('curl_muilt',TRUE);
        	foreach($rs as $key=>$val)
        	{


				$url_arr = array();

				for($i=1;$i<=intval($val['pageNum']);$i++){
/*防止封IP，特暂停4s钟*/
if($i%10==0)
{
	sleep(31);
}

					if($i==1)
					{
						$pageurl = $val['classHref'].'index.html';
					}else{
						$pageurl = $val['classHref'].'index_'.$i.'.html';
					}
var_dump("正在采集:".$pageurl."<br>");

					/*抓取页面*/
					$url_arr = array();
					$url_arr[] = $pageurl;
					$page = $curl_muilt->remote($url_arr);

					/*过滤出标题部分*/
					$pattern = '/<div class="c_c">(.*)<div class="c_p_s">/isU';
					preg_match_all($pattern,$page[0],$out);
					$url_arr = $out[1][0];

					/*提取出标题和url地址*/
					$pattern = '/href="(.*)" title="(.*)">(.*)<\/a>/isU';
					preg_match_all($pattern,$url_arr,$out2);

					$url_arr = $out2[1];
					$title_arr = $out2[3];
					$page_content = $curl_muilt->remote($url_arr);
					$content = array();
					foreach($page_content as $pageXiangqing)
					{
						$pattern = '/<div class="c_t">(.*)<div class="c_a_2">/isU';
						preg_match_all($pattern,$pageXiangqing,$pageContent);
						$content[] = $pageContent[1][0];
					}

					foreach($content as $pk=>$pv)
					{
						$insert_data = array('classid'=>$key,
												'content'=>trim(htmlspecialchars($pv)),
												'title'=>htmlspecialchars($title_arr[$pk]),
												'href'=>$url_arr[$pk]);
						$db->insert('articles',$insert_data);
					}
//					//数组分割
//					$new_url_arr = array_chunk($url_arr,1,true);
//					foreach($new_url_arr as $k=>$v)
//					{
//						$page_xiangqing = $curl_muilt->remote($v);
//
//						foreach($page_xiangqing as $pageXiangqing)
//						{
//							$pattern = '/<div class="c_t">(.*)<div class="c_a_2">/isU';
//							preg_match_all($pattern,$pageXiangqing,$pageContent);
//							$content = $pageContent[1][0];
//							$insert_data = array('classid'=>$key,
//												'content'=>trim(htmlspecialchars($content)),
//												'title'=>htmlspecialchars($title_arr[$k]),
//												'href'=>$url_arr[$k]);
//							$db->insert('articles',$insert_data);
//						}
//					}
ob_flush();
flush();
				}
			}
			
        }


}
?>
